import DatabaseUtils from "../utils/db-utils";

class ShareDao {

    /**
     * 查询对局分享数据
     * @param where
     */
    static queryShareTable = async (where: any[]) => {
        const tableName = 't_share';
        const columns = ['id', 'battle_id', 'user_id', 'user_name', 'share_code', 'validity_day', 'share_password', 'create_time', 'update_time'];
        const sqlObj = DatabaseUtils.getDynamicSelectObj(tableName, columns, where);
        return await DatabaseUtils.execSql(sqlObj.sql, sqlObj.params);
    };

    /**
     * 根据分享码获取分享信息
     * @param shareCode
     */
    static getShareByCode = async (shareCode: string) => {
        const sql = `select id, battle_id as battleId, user_id as userId, user_name as userName, share_code as shareCode,
                 validity_day as validityDay, share_password as sharePassword, create_time as createTime, update_time as updateTime 
                from t_share where share_code = ?`;
        const params = [shareCode];
        const rows: any = await DatabaseUtils.execSql(sql, params);
        return rows.length === 0 ? null : rows[0];
    }

    /**
     * 保存分享数据
     * @param battleId
     * @param userId
     * @param userName
     * @param shareCode
     * @param validityDay
     * @param sharePassword
     */
    static saveShare = async (battleId: string, userId: string, userName: string, shareCode: string,
                              validityDay: number | null, sharePassword: string | null) => {
        const sql = `insert into t_share(battle_id, user_id, user_name, share_code, validity_day, share_password) values(?, ?, ? ,?, ?, ?)`;
        const params = [battleId, userId, userName, shareCode, validityDay, sharePassword];
        await DatabaseUtils.execSql(sql, params);
    }
}

export default ShareDao;